﻿using System;
using System.Globalization;
using System.Web.Security;
using Monkey.Warrenty.Biz;
using Monkey.Warrenty.Constants;
using Monkey.Warrenty.EntityModel;
using MonkeyWarranty.Web.Common;

namespace MonkeyWarranty.Web.Employees
{
    public partial class New : System.Web.UI.Page
    {
        #region Properties

        private EmployeeBiz _employeeBiz;
        private EmployeeBiz EmployeeManager
        {
            get { return _employeeBiz ?? (_employeeBiz = new EmployeeBiz()); }
        }

        #endregion

        #region Events

        protected void Page_Load(object sender, EventArgs e)
        {
            if (WarrantyCommon.CurrrentEmployee == null)
            {
                Response.Redirect("~/Logon.aspx");
                return;
            }

            if (!WarrantyCommon.CurrrentEmployee.HasRole(RoleConstant.Admin))
            {
                Response.Redirect("~/NoPermission.aspx");
            }

            if (!IsPostBack)
            {
                // get auto code
                var nextCode = EmployeeManager.GetNextCode();
                txtCode.Text = string.Format("{0:00000}", nextCode);
                txtSortOrder.Text = nextCode.ToString(CultureInfo.InvariantCulture);
            }
        }

        protected void btnSave_Click(object sender, EventArgs e)
        {
            lblErrorMessage.Text = string.Empty;
            errorMessage.Visible = false;

            // validate inputs
            var code = txtCode.Text.Trim();
            if (code.Length == 0)
            {
                lblErrorMessage.Text = Resources.GlobalStrings.CodeIsRequired;
                errorMessage.Visible = true;
                return;
            }
            var name = txtName.Text.Trim();
            if (name.Length == 0)
            {
                lblErrorMessage.Text = Resources.GlobalStrings.NameIsRequired;
                errorMessage.Visible = true;
                return;
            }
            var email = txtEmailAddress.Text.Trim();
            var phone = txtPhoneNumber.Text.Trim();
            var descrition = txtDescription.Text.Trim();
            int sortOrder;
            int.TryParse(txtSortOrder.Text.Trim(), out sortOrder);
            var userName = txtUserName.Text.Trim();
            var password = FormsAuthentication.HashPasswordForStoringInConfigFile(txtPassword.Text, "SHA1");

            var employee = new Employee
            {
                Code = code,
                Name = name,
                UserName = userName,
                Password = password,
                MailAddress = email,
                PhoneNumber = phone,
                Description = descrition,
                SortOrder = sortOrder,
                Hidden = chkHidden.Checked
            };

            try
            {
                employee = EmployeeManager.CreateEmployee(employee);
            }
            catch (ApplicationException ex)
            {
                lblErrorMessage.Text = ex.Message;
                errorMessage.Visible = true;
                return;
            }
            
            if (employee == null)
            {
                lblErrorMessage.Text = Resources.GlobalStrings.CreateFailed;
                errorMessage.Visible = true;
                return;
            }

            // success
            Response.Redirect("List.aspx");
        }

        #endregion
    }
}